package com.tbeasy.financial.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tbeasy.financial.R;
import com.tbeasy.financial.data.BudgetBean;

public class BudgetItemAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private ArrayList<BudgetBean> items;
	private Context mContext;

	public BudgetItemAdapter(Context context, ArrayList<BudgetBean> items) {
		mContext = context;
		this.items = items;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public BudgetBean getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if(convertView == null) {
			convertView = inflater.inflate(R.layout.budget_listview_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.setValue(getItem(position));
		
		return convertView;
	}

	public final class ViewHolder {
		
		private ImageView typeImage;
		private TextView typeTextView;
		private TextView valueTextView;
		private TextView balanceTextView;
		
		public ViewHolder(View view) {
			typeImage = (ImageView) view.findViewById(R.id.budget_listview_item_icon);
			typeTextView = (TextView) view.findViewById(R.id.budget_item_type_name);
			valueTextView = (TextView) view.findViewById(R.id.budget_item_budget_value);
			balanceTextView = (TextView) view.findViewById(R.id.budget_item_budget_balance);
		}
		
		public void setValue(BudgetBean data) {
			typeImage.setImageResource(R.drawable.icon_yfsp);
			typeTextView.setText(data.getBugetName());
			valueTextView.setText(String.format(mContext.getText(R.string.budget_value).toString(), data.getBudgetValue()));
			balanceTextView.setText(String.format(mContext.getText(R.string.balance_value).toString(), data.getBudgetValue()));
		}
		
	}
	
	public void setItems(ArrayList<BudgetBean> items) {
		this.items = items;
		notifyDataSetChanged();
	}
}
